<?php

function manage_chat_list_page()
{
    @$output = drupal_render(drupal_get_form('manage_chat_list_form'));
    return $output;
}
function manage_chat_groups_list_page(){
    @$output = drupal_render(drupal_get_form('manage_chat_groups_list_form'));
    return $output;
}

function manage_chat_list_form($form, &$form_state)
{
    global $user;
    $form['nickname'] = array(
        '#type' => 'textfield',
        '#default_value' => '',
        '#attributes' => array('placeholder' => '好友姓名'),
        '#validated' => TRUE,
    );

    $res = db_select('chat_groups', 'CG')
        ->fields('CG', array('chat_group_id', 'chat_group_name'))
        ->condition('CG.chat_group_id',array(2,3),'not in')
        ->condition(db_or()->condition('CG.user_id',$user->uid,'=')->condition('CG.chat_group_id', 1, '='))
        ->execute();
    $options = ['' => ''];
    foreach ($res as $record) {
        $options[$record->chat_group_id] = $record->chat_group_name;
    }
    $form['chat_group'] = array(
        '#type' => 'select',
        '#default_value' => '',
        '#validated' => TRUE,
        '#options'=> $options,
    );

    unset($options['']);
    $form['chat_group_assign'] = array(
        '#type' => 'select',
        '#default_value' => '1',
        '#validated' => TRUE,
        '#options'=> $options,
    );

    $form['button'] = array(
        '#type' => 'button',
        '#value' => '保存',
        '#attributes' => array('onclick' => "editfriend('group','','','')"),
    );

    $table['#markup'] =manage_chat_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function manage_chat_list_view_page($form, &$form_state)
{
        global $user;
        $output = '';
        $header = array(
            array('data' => '选择', 'class' => ['no']),
            array('data' => '好友名称', 'class' => ['nickname']),
            array('data' => '好友分组', 'class' => ['group'], 'field' => 'chat_group_name'),
            array('data' => '操作', 'class' => ['cz']),
        );
        $rows = array();
        $query = db_select('chat_users', 'CU')->extend('PagerDefault')->extend('TableSort');
        $query->join('chat', 'C', 'C.chat_id = CU.chat_id');
        $query->join('chat_users', 'CU1', 'CU.chat_id = CU1.chat_id');
        $query->join('chat_groups', 'CG', 'CG.chat_group_id = CU.chat_group_id');

        if(!empty($form_state['values']['nickname'])){
            $query->condition('CU1.nickname', '%' . db_like($form_state['values']['nickname']) . '%', 'LIKE');
        }
        if (!empty($form_state['values']['chat_group'])) {
            $query->condition('CU.chat_group_id', $form_state['values']['chat_group'],  '=');
        }

        $query->condition('C.type', 0, '=');
        $query->condition('CU.user_id', $user->uid, '=');
        $query->condition('CU1.user_id', $user->uid, '!=');
        $query->condition('CU.active', '1', '=');
        $query->Fields('CU1',array('chat_id','user_id','nickname'));
        $query->addField('CG', 'chat_group_name');
        $query->groupBy('CU1.user_id');
        $query->limit(variable_get("list_page_item_number", 5));
        $result = $query->orderByHeader($header)->execute();
        $n = 1;
        foreach ($result as $row) {
            $rows[] = array(
                array('data' => '<input type="checkbox" class="listViewTableCheck" value ="' . $row->chat_id . '"/>' . $n++, 'class' => ['integrant essential no '.$row->chat_id]),
                array('data' => $row->nickname, 'class' => ['essential nickname '.$row->chat_id]),
                array('data' => $row->chat_group_name, 'class' => ['essential chat_group_name '.$row->chat_id]),
                array('data' => '<img width="100px" height="25px" src="'.base_path().'sites/all/themes/tweme/images/images/del-fri.png" style="cursor: pointer;" onclick="editfriend(\'del\',\''.$row->user_id.'\',\''.$row->nickname.'\',\''.$row->chat_id.'\')"/>', 'class' => 'cz '.$row->chat_id),
            );
        }
        $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover'))));
        $output .= theme('pager');
        return $output;
    // }
}


function manage_chat_groups_list_form($form, &$form_state)
{

    $table['#markup'] =manage_group_list_view_page($form, $form_state);
    $form['table'] =$table;
    return $form;
}

function manage_group_list_view_page($form, &$form_state)
{
    global $user;
    $output = '';
    $header = array(
        array('data' => '选择', 'class' => ['no']),
        array('data' => '分组名称', 'class' => ['group'], 'field' => 'chat_group_name'),
        array('data' => '操作', 'class' => ['cz']),
    );
    $rows = array();
    $query = db_select('chat_groups', 'CG')->extend('PagerDefault')->extend('TableSort');
    $query->fields('CG', array('chat_group_id', 'chat_group_name'));
    $query->condition('CG.user_id',$user->uid,'=');
//    $query->groupBy('CU1.user_id');
    $query->limit(variable_get("list_page_item_number", 5));
    $result = $query->orderByHeader($header)->execute();
    $n = 1;
    foreach ($result as $row) {
        $rows[] = array(
            array('data' => '<input type="checkbox" class="listViewTableCheck" value ="' . $row->chat_group_id . '"/>' . $n++, 'class' => ['integrant essential no '.$row->chat_group_id]),
            array('data' => $row->chat_group_name, 'class' => ['essential chat_group_name '.$row->chat_group_id]),
            array('data' => '<a href="'.base_path().'manage_chat/groups/edit/'.$row->chat_group_id.'"><img width="25px" height="25px" src="'.base_path().'sites/all/themes/tweme/images/images/cz.png" style="cursor: pointer;"/></a>', 'class' => 'more '.$row->chat_group_id),
        );
    }

    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover'))));
    $output .= theme('pager');
    return $output;
    // }
}

